﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
//using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using System.IO;
using System.Security.Cryptography;

namespace Server
{
    
    public class CCryptography
    {
        #region Thuoc Tinh
        /// <summary>
        /// Thuật toán được sử dụng để mã hóa
        /// </summary>
        private const string _thuattoanmahoa = "DpapiSymmetricCryptoProvider";
        /// <summary>
        /// 
        /// </summary>
        //private static CryptographyManager _crypt = EnterpriseLibraryContainer.Current.GetInstance<CryptographyManager>();
        #endregion

        #region Phuong Thuc
        /// <summary>
        /// Mã hóa dữ liệu
        /// </summary>
        /// <param name="str">Chuỗi cần mã hóa</param>
        /// <returns></returns>
        public static string MaHoa(string str)
        {
            try
            {
                //Mã hóa dữ liệu
                //string temp = _crypt.EncryptSymmetric(_thuattoanmahoa, str);
                //string temp = str;
                //return temp;

                byte[] Key = Encoding.UTF8.GetBytes("chungtalanhungluquyotanrungxanh0");
                byte[] IV = Encoding.UTF8.GetBytes("chungtalanhunglu");
                if (str == null || str.Length <= 0)
                    throw new ArgumentNullException("plainText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");
                byte[] encrypted;

                using (Rijndael _thuattoanRij = Rijndael.Create())
                {
                    _thuattoanRij.Key = Key;
                    _thuattoanRij.IV = IV;

                    ICryptoTransform _mahoa = _thuattoanRij.CreateEncryptor(_thuattoanRij.Key, _thuattoanRij.IV);

                    using (MemoryStream _msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream _csEncrypt = new CryptoStream(_msEncrypt, _mahoa, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(_csEncrypt))
                            {

                                swEncrypt.Write(str);
                            }
                            encrypted = _msEncrypt.ToArray();
                        }
                    }
                }


                return Convert.ToBase64String(encrypted);
            }
            catch (Exception c)
            {
                throw new Exception("Không thể mã hóa dữ liệu");
            }
        }

        /// <summary>
        /// Giải mã dữ liệu
        /// </summary>
        /// <param name="str">Chuỗi cần giải mã</param>
        /// <returns></returns>
        public static string GiaiMa(string str)
        {
            try
            {
                ////Giải mã dữ liệu
                ////string temp = _crypt.DecryptSymmetric(_thuattoanmahoa, str);
                //string temp = str;
                //return temp;
                byte[] cipherText = Convert.FromBase64String(str);
                byte[] Key = Encoding.UTF8.GetBytes("chungtalanhungluquyotanrungxanh0");
                byte[] IV = Encoding.UTF8.GetBytes("chungtalanhunglu");
                if (cipherText == null || cipherText.Length <= 0)
                    throw new ArgumentNullException("cipherText");
                if (Key == null || Key.Length <= 0)
                    throw new ArgumentNullException("Key");
                if (IV == null || IV.Length <= 0)
                    throw new ArgumentNullException("Key");

                string plaintext = null;

                using (Rijndael _thuattoanRij = Rijndael.Create())
                {
                    _thuattoanRij.Key = Key;
                    _thuattoanRij.IV = IV;

                    ICryptoTransform decryptor = _thuattoanRij.CreateDecryptor(_thuattoanRij.Key, _thuattoanRij.IV);

                    using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                plaintext = srDecrypt.ReadToEnd();
                            }
                        }
                    }
                }
                return plaintext;
            }
            catch (Exception c)
            {
                throw new Exception("Không thể giải mã dữ liệu");
            }
        }
        #endregion
    }
}
